<script setup>
import { ref, nextTick } from 'vue'
import ChannelSelect from './ChannelSelect.vue'
import { Plus } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { useArticleStore } from '@/stores'

const useArticle = useArticleStore()
// 控制抽屉显示隐藏
const visibleDrawer = ref(false)

// 默认表单数据
const defaultForm = {
  title: '', //标题
  cate_id: '', //id
  cover_img: '', //图片地址
  content: '', //文章内容
  state: '', //状态
}
// 准备数据
const formModel = ref({
  ...defaultForm,
})
// 图片上传相关逻辑
const imageUrl = ref('')
const onSelectFile = (uploadFile) => {
  // 获取到上传的图片地址
  console.log(uploadFile.raw)
  imageUrl.value = URL.createObjectURL(uploadFile.raw) //预览图片
}

// 提交

const onPublish = (state) => {
  formModel.value.cover_img = imageUrl.value
  console.log(formModel.value)

  // 将已发布还是草稿存入formModel
  formModel.value.state = state

  // // 将对象转换成 formData对象
  // const fd = new FormData()
  // for (let key in formModel.value) {
  //   fd.append(key, formModel.value[key])
  // }
  // 发请求
  if (formModel.value.id) {
    // 编辑操作
    useArticle.editArticleList(formModel.value)
    formModel.value = { ...defaultForm } //基于默认的数据重置form数据
  } else {
    // 添加操作
    useArticle.addArticleList(formModel.value)
    // 通知父组件，添加成功
    // emit('success', 'add')
  }
  visibleDrawer.value = false
}
// 组件对我暴露一个方法 open ，基于open传来的参数，区分添加还是编辑
// open({}) => 表单无需渲染，说明是添加
// open({id, ...,...}) => 表单需要渲染，说明是编辑
// open 调用后，可以打开弹窗
// 获得富文本组件
const editorRef = ref()
const open = async (row) => {
  // console.log(row)
  // 回显图片
  imageUrl.value = row.cover_img
  if (row.id) {
    console.log('编辑回显')
    // 表单回显内容
    formModel.value = row
  } else {
    console.log('添加回显')
    formModel.value = { ...defaultForm } //基于默认的数据重置form数据
    // 等待 DOM 更新，确保富文本实例已创建
    await nextTick()
    if (editorRef.value) {
      editorRef.value.setHTML('') // 如果富文本有内容就清空富文本
    }
  }
  // 显示抽屉
  visibleDrawer.value = true
}
defineExpose({ open })
</script>
<template>
  <el-drawer
    v-model="visibleDrawer"
    :title="formModel.id ? '编辑文章' : '添加文章'"
    direction="rtl"
    size="50%"
  >
    <!-- 发表文章表单 -->
    <el-form :model="formModel" ref="formRef" label-width="100px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formModel.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <channel-select v-model="formModel.cate_id" width="100%"></channel-select>
      </el-form-item>
      <el-form-item label="文章封面" prop="cover_img">
        <!-- 此处需要关闭element-plus 的自动上传， 不需要配置 action 等参数
        只需要做前端的本地预览图片即可，无需在提交前上传图标
         语法： URl.createObjectURl(...)创建本地预览的地址，来浏览
        -->
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :auto-upload="false"
          :on-change="onSelectFile"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <div class="editor">
          <quill-editor
            ref="editorRef"
            v-model:content="formModel.content"
            theme="snow"
            contentType="html"
          >
          </quill-editor>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onPublish('已发布')">发布</el-button>
        <el-button type="info" @click="onPublish('草稿')">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>
<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 500px;
  }
}
</style>
